<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>智慧工地</title>
		<link rel="stylesheet" href="../../../css/mui.min.css" />
		<link rel="stylesheet" type="text/css" href="../../../css/icons-extra.css" />
		<link rel="stylesheet" type="text/css" href="../../../css/master.css" />
		<link rel="stylesheet" type="text/css" href="../../../css/news-list.css" />
		<link rel="stylesheet" type="text/css" href="../../../css/refresh.css" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav mui-head bg-00BC8D">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title text-ffffff">录入人员信息</h1>
		</header>
		<div class="mui-content" id="app">

			<div id="dcontent" class="dcontent">
				<div class="button" onclick="getImage()">拍照</div>
				<br/>
				<img id="tp" src="" width="100%" />
				<br/>
			</div>

			<input type="button" value="点击上传图片" />
		</div>

	</body>
	<script src="../../../js/jquery.min.js"></script>
	<script src="../../../js/mui.min.js"></script>
	<script src="../../../js/mui.picker.min.js"></script>
	<script src="../../../libs/waitMe/waitMe.min.js"></script>
	<script src="../../../js/setting.js"></script>
	<script src="../../../js/common.js"></script>
	<script src="../../../js/vue.min.js"></script>
	<script src="schoolpersonnel.js"></script>

</html>
<script>
	var i = 1,
		gentry = null,
		w = null;
	var hl = null,
		le = null,
		de = null,
		ie = null;
	var unv = true;
	var bUpdated = false; //用于兼容可能提前注入导致DOM未解析完更新的问题
	// H5 plus事件处理
	function plusReady() {
		// 获取摄像头目录对象
		plus.io.resolveLocalFileSystemURL('_doc/', function(entry) {
			entry.getDirectory('camera', {
				create: true
			}, function(dir) {
				gentry = dir;
				updateHistory();
			}, function(e) {
				outSet('Get directory "camera" failed: ' + e.message);
			});
		}, function(e) {
			outSet('Resolve "_doc/" failed: ' + e.message);
		});
	}
	if(window.plus) {
		plusReady();
	} else {
		document.addEventListener('plusready', plusReady, false);
	}
	// 监听DOMContentLoaded事件
	document.addEventListener('DOMContentLoaded', function() {
		// 获取DOM元素对象
		hl = document.getElementById('history');
		le = document.getElementById('empty');
		de = document.getElementById('display');
		if(ie = document.getElementById('index')) {
			ie.onchange = indexChanged;
		}
		// 判断是否支持video标签
		unv = !document.createElement('video').canPlayType;
		updateHistory();
	}, false);

	function changeIndex() {
		outSet('选择摄像头：');
		ie.focus();
	}

	function indexChanged() {
		de.innerText = ie.options[ie.selectedIndex].innerText;
		i = parseInt(ie.value);
		alert(de.innerText);
	}
	// 拍照
	function getImage() {
		alert('开始拍照：');
		var cmr = plus.camera.getCamera();
		cmr.captureImage(function(p) {
			alert('成功：' + p);
			plus.io.resolveLocalFileSystemURL(p, function(entry) {
				displayFile(entry);
			}, function(e) {
				alert('读取拍照文件错误：' + e.message);
			});
		}, function(e) {
			alert('失败：' + e.message);
		}, {
			filename: '_doc/camera/',
			index: 1
		});
	}

	/*function convertImgToBase64(url, callback, outputFormat) {
		alert("进入方法");
		var canvas = document.createElement('CANVAS'),
			ctx = canvas.getContext('2d'),
			img = new Image;
		img.crossOrigin = 'Anonymous';
		img.onload = function() {
			canvas.height = img.height;
			canvas.width = img.width;
			ctx.drawImage(img, 0, 0);
			var dataURL = canvas.toDataURL(outputFormat || 'image/png');
			callback.call(this, dataURL);
			canvas = null;
		};
		img.src = url;
		alert(url);
	}*/

	function dataURItoBlob(base64Data) {
		alert("开始转化");
		var byteString;
		if(base64Data.split(',')[0].indexOf('base64') >= 0)
			byteString = atob(base64Data.split(',')[1]);
		else
			byteString = unescape(base64Data.split(',')[1]);
		var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];
		var ia = new Uint8Array(byteString.length);
		for(var i = 0; i < byteString.length; i++) {
			ia[i] = byteString.charCodeAt(i);
		}
		return new Blob([ia], {
			type: mimeString
		});
	}

	// 显示文件
	function displayFile(entry) {
		/*convertImgToBase64(entry.toLocalURL(), function(base64Img) {
			alert("转化图片");
			console.log(base64Img);
			searchList(base64Img, base64Img);
			//$("#tp").attr("src", entry.toLocalURL());
		});*/

		var blob = dataURItoBlob(entry.toLocalURL()); // 上一步中的函数
		var canvas = document.createElement('canvas');
		var dataURL = canvas.toDataURL('image/jpeg', 0.5);
		var fd = new FormData(document.forms[0]);
		fd.append("idCarFile", blob, 'image.png');
		fd.append("regFile", blob, 'image.png');
		alert("开始请求");
		searchList(fd);
		
		/*if(w) {
			alert('重复点击！');
			return;
		}
		if(!li || !li.entry) {
			ouSet('无效的媒体文件');
			return;
		}
		var name = li.entry.name;
		var suffix = name.substr(name.lastIndexOf('.'));
		var url = '';
		if(suffix == '.mov' || suffix == '.3gp' || suffix == '.mp4' || suffix == '.avi') {
			//if(unv){plus.runtime.openFile('_doc/camera/'+name);return;}
			url = '/plus/camera_video.html';
		} else {
			url = '/plus/camera_image.html';
		}
		w = plus.webview.create(url, url, {
			hardwareAccelerated: true,
			scrollIndicator: 'none',
			scalable: true,
			bounce: 'all'
		});
		w.addEventListener('loaded', function() {
			w.evalJS('loadMedia("' + li.entry.toLocalURL() + '")');
			//w.evalJS('loadMedia("'+'http://localhost:13131/_doc/camera/'+name+'")');
		}, false);
		w.addEventListener('close', function() {
			w = null;
		}, false);
		w.show('pop-in');*/
	}

	mui.plusReady(function() { //如果需要调用plus
		searchList(null); //初始化
	});

	function searchList(fd) {
		if(fd == null) {
			return;
		}
		var datebegin = "";
		var dateend = "";
		var xhr = null;
		var url = "http://ai-gongdi.com/add/person!inputPerson";
		var data = fd;
		alert(data);
		xhr = new plus.net.XMLHttpRequest();
		xhr.open("POST", url);
		xhr.setRequestHeader('Content-Type', 'application/json');
		xhr.send(JSON.stringify(data));
		xhr.onreadystatechange = function() {
			alert(xhr.readyState);
			if(xhr.readyState == 4) {
				alert(xhr.status);
				if(xhr.status == 200) {
					alert(xhr.responseText);
					console.log(xhr.responseText);
					//app.items.concat(eval('(' + xhr.responseText + ')'));
				}
			}
		}
	}
</script>
</script>